package com.lilosoft.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.MybatisConfiguration;
import com.baomidou.mybatisplus.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.mapper.LogicSqlInjector;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * MybatisPlus配置
 *
 * @author qiusheng
 * @Date 2018-10-30
 */
@Configuration
@MapperScan(basePackages = {"com.lilosoft.business.cga.dao"}, sqlSessionFactoryRef = "sqlSessionFactoryPrimary")
public class MybatisPlusConfig {

	@Autowired
	DruidProperties druidProperties;

    @Bean(name = "dataSourcePrimary")
    @Primary
    public DataSource dataSourcePrimary() {
        DruidDataSource dataSource = new DruidDataSource();
        druidProperties.config(dataSource);
        return dataSource;
    }

    @Bean(name = "transactionManagerPrimary")
    @Primary
    public DataSourceTransactionManager TransactionManager() {
        return new DataSourceTransactionManager(dataSourcePrimary());
    }

    @Bean(name = "sqlSessionFactoryPrimary")
    @Primary
    public SqlSessionFactory AdminSqlSessionFactory(@Qualifier("dataSourcePrimary") DataSource dataSourcePrimary) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSourcePrimary);
        sqlSessionFactory.setTypeAliasesPackage("com.lilosoft.business.cga.models");
        sqlSessionFactory.setMapperLocations(
        		new PathMatchingResourcePatternResolver()
        		.getResources("classpath*:com/lilosoft/business/cga/dao/mapper/*Mapper.xml"));

        //configure配置
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setMapUnderscoreToCamelCase(false);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        sqlSessionFactory.setConfiguration(configuration);

        //plugins插件配置
        sqlSessionFactory.setPlugins(new Interceptor[]{new PaginationInterceptor()});

        //global全局配置
        sqlSessionFactory.setGlobalConfig(globalConfiguration());

        return sqlSessionFactory.getObject();
    }

    @Bean
    public GlobalConfiguration globalConfiguration() {
        //参考mybatisplus官方文档的全局配置
        // http://baomidou.oschina.io/mybatis-plus-doc/#/api?id=globalconfig
        GlobalConfiguration conf = new GlobalConfiguration(new LogicSqlInjector());
        conf.setLogicDeleteValue("0");
        conf.setLogicNotDeleteValue("1");
        conf.setIdType(0);
        conf.setDbColumnUnderline(true);
        conf.setRefresh(true);
        conf.setFieldStrategy(2);
        return conf;
    }

}
